package com.example.test0520.activity;

import android.app.Activity;
import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;
import com.example.test0520.R;
import com.example.test0520.util.ScreenUtils;

import skin.support.SkinCompatManager;

public class SetActivity extends BaseActivity {

    private ImageButton setBack;
    private ImageView iconSet;
    private RelativeLayout instr;
    private RelativeLayout proposalLayout;
    private RelativeLayout skinLayout;
    private RelativeLayout fkLayout;


    @Override
    protected int initLayout() {
        return R.layout.activity_set;
    }

    @Override
    protected void initView() {
        setBack = (ImageButton) findViewById(R.id.set_back);
        iconSet = (ImageView) findViewById(R.id.icon_set);
        instr = (RelativeLayout) findViewById(R.id.instr);
        proposalLayout = (RelativeLayout) findViewById(R.id.proposal_layout);
        skinLayout = (RelativeLayout) findViewById(R.id.skin_layout);
        fkLayout = (RelativeLayout) findViewById(R.id.fk_layout);
    }

    @Override
    protected void initData() {
        getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
        Glide.with(getApplicationContext()).load(R.drawable.ic_tb).apply(RequestOptions.bitmapTransform(new RoundedCorners(15))).into(iconSet);
        instr.setOnClickListener(v -> startActivity(new Intent(SetActivity.this, OperationActivity.class)));
        //返回
        setBack.setOnClickListener(v -> onBackPressed());
        //联系作者
        proposalLayout.setOnClickListener(v -> {
            //创建弹窗
            View inflate = LayoutInflater.from(getApplicationContext()).inflate(R.layout.dialog_item, null, false);
            AlertDialog dialog = new AlertDialog.Builder(SetActivity.this).setView(inflate).create();
            dialog.setCancelable(false);
            Window window = dialog.getWindow();
            window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
            window.setGravity(Gravity.CENTER);
            TextView ok = inflate.findViewById(R.id.ok);
            ok.setOnClickListener(v1 -> {
                //将文本复制到剪切板上
                ClipboardManager cm = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
                ClipData label = ClipData.newPlainText("Label", "1350465671@qq.com");
                cm.setPrimaryClip(label);
                dialog.dismiss();
            });
            dialog.show();
            window.setLayout((ScreenUtils.getScreenWidth(getApplicationContext()) / 4 * 3), LinearLayout.LayoutParams.WRAP_CONTENT);
//                window.setDimAmount(0.1f);
        });

        skinLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SharedPreferences sp = getSharedPreferences("skin", MODE_PRIVATE);
                String skin = sp.getString("skinName", "");
                if (skin.equals("night")){
                    // 恢复应用默认皮肤
                    SkinCompatManager.getInstance().restoreDefaultTheme();
                    SharedPreferences.Editor edit = sp.edit();
                    edit.putString("skinName","default");
                    edit.commit();
                }else{
                    // 指定皮肤插件
                    SkinCompatManager.getInstance().loadSkin("night", SkinCompatManager.SKIN_LOADER_STRATEGY_BUILD_IN); // 后缀加载
                    SharedPreferences.Editor edit = sp.edit();
                    edit.putString("skinName","night");
                    edit.commit();
                }
            }
        });
    }

}